package com.gms.controller.placemanager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gms.entity.place.*;
import com.gms.entity.user.User;
import com.gms.service.placemanager.PlaceApplyService;
import com.gms.service.placemanager.PlaceFeeStanderService;
import com.gms.service.placemanager.PlaceNoticeService;
import com.gms.service.placemanager.PlaceService;
import com.gms.util.ObtainDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class PlaceManagerController {

    @Autowired
    PlaceService placeService;
    @Autowired
    PlaceApplyService placeApplyService;
    @Autowired
    PlaceFeeStanderService placeFeeStanderService;
    @Autowired
    PlaceNoticeService placeNoticeService;

    public static final int PAGE_SIZE=8;

    /*
     * 场地功能
     *
     * */
    //去场地详情页面
    @GetMapping("/toPlaceDetail")
    public String toPlaceDetail(){
        return "placemodal/placedetail";
    }

    //得到场地信息
    @GetMapping("/getPlaces")
    @ResponseBody
    public PlaceTest1 getPlaces(Integer pageNum){
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<Place> placeList = placeService.SelectAllPlace();
        PageInfo page = new PageInfo(placeList);
        PlaceTest1 placeTest1 = new PlaceTest1();
        placeTest1.setPlaceList(placeList);
        placeTest1.setPages(page);
        return placeTest1;
    }

    //新增场地
    @PostMapping("/addPlace")
    public String AddPlace(Place place, HttpSession session){
        place.setPlace_state("可使用");
        place.setPlace_leader((String) session.getAttribute("username"));
        //插入数据
        placeService.InsertPlace(place);
        //返回场地信息页面
        return "redirect:/toPlaceDetail";
    }

    //检测场地编号是否已存在
    @RequestMapping("/checkPlaceNo")
    @ResponseBody
    public boolean checkPlaceNo(String placeno){
        return placeService.CheckPlaceNo(placeno);
    }

    //删除场地
    @RequestMapping("/deletePlace")
    @ResponseBody
    public int DeletePlace(String placeno){
        placeService.DeletePlaceByNo(placeno);
        //返回场地信息页面
        return 1;
    }

    //查询对应楼层及对应类型的场地
    @RequestMapping("/searchPlace")
    @ResponseBody
    public PlaceTest1 searchPlace(int pageNum, String placestorey, String placetype){
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<Place> placeList = placeService.SelectAllPlaceByOrder(placetype,placestorey);
        PageInfo page = new PageInfo(placeList);
        PlaceTest1 placeTest1 = new PlaceTest1();
        placeTest1.setPlaceList(placeList);
        placeTest1.setPages(page);
        return placeTest1;
    }

    /*
    * 预约功能
    *
    * */
    @GetMapping("/toPlaceApply")
    public String toPlaceApply() {
        placeApplyService.UpdateAllPlaceApply();
        return "placemodal/placeapply";
    }

    @GetMapping("/toPlaceApply1")
    public String toPlaceApply1() {
        placeApplyService.UpdateAllPlaceApply();
        return "placemodal/placeapply1";
    }

    //查询可预约场地
    @PostMapping("/researchApply")
    @ResponseBody
    public List<PlaceApply> researchApply(){
        List<Place> listaim = placeService.SelectAllPlaceByOrder(null,null);
        //查询出来的可预约信息
        List<PlaceApply> listfinal = new ArrayList<>();
        Date date = ObtainDate.tomorrowDate();//获取明天时间
        for ( int i = 0 ;i<listaim.size();i++){
            //获取时间链表
            List<String> listtime = ObtainDate.timeList();
            //获取预约链表
            List<PlaceApply> list = placeApplyService.QueryPlaceApplyByPage(listaim.get(i).getPlace_no(),date);
            //进行时间段匹配
            for ( int j = 0;j < list.size();j++){
                for(int k = 0 ; k<listtime.size(); k++ ){
                    if(list.get(j).getApply_period().equals(listtime.get(k))){
                        listtime.remove(k);
                    }
                }
            }
            if(listtime.size() != 0){
                for ( int l = 0 ; l<listtime.size();l++){
                    PlaceApply placeApply = new PlaceApply();
                    placeApply.setPlace_no(listaim.get(i).getPlace_no());
                    placeApply.setPlace_type(listaim.get(i).getPlace_type());
                    placeApply.setPlace_storey(listaim.get(i).getPlace_storey());
                    placeApply.setApply_date(date);
                    placeApply.setApply_period(listtime.get(l));
                    listfinal.add(placeApply);
                }
            }
        }
        return listfinal;
    }

    //查询
    @PostMapping("/researchApply1")
    @ResponseBody
    public List<PlaceApply> researchApply1(@RequestBody PlaceTest placetest) throws ParseException {
        List<Place> listaim = placeService.SelectAllPlaceByOrder(placetest.getPlacetype(),placetest.getPlacestorey());
        //存放查询出来的可预约信息
        List<PlaceApply> listfinal = new ArrayList<>();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

        for(int i = 0 ; i < listaim.size(); i++){
            //有选择时间
            if(placetest.getPlaceDate().length!=0){
                for(int m=0;m<placetest.getPlaceDate().length;m++){
                    Date date = dateformat.parse(placetest.getPlaceDate()[m]);

                    List<String> listtime = new ArrayList<>();
                    if(placetest.getFtimeList().length==0){
                        listtime = ObtainDate.timeList();
                    }else {
                        //获取时间链表
                        for(int g=0;g<placetest.getFtimeList().length;g++){
                            listtime.add(placetest.getFtimeList()[g]);
                        }
                    }

                    //获取预约链表
                    List<PlaceApply> list = placeApplyService.QueryPlaceApplyByPage(listaim.get(i).getPlace_no(),date);

                    System.out.println("查询结果："+list.toString());

                    //进行时间段匹配
                    for ( int j = 0;j < list.size();j++){
                        for(int k = 0 ; k<listtime.size(); k++ ){
                            if(list.get(j).getApply_period().equals(listtime.get(k))){
                                listtime.remove(k);
                            }
                        }
                    }
                    if(listtime.size() != 0){
                        for ( int l = 0 ; l<listtime.size();l++){
                            PlaceApply placeApply = new PlaceApply();
                            placeApply.setPlace_no(listaim.get(i).getPlace_no());
                            placeApply.setPlace_type(listaim.get(i).getPlace_type());
                            placeApply.setPlace_storey(listaim.get(i).getPlace_storey());
                            placeApply.setApply_date(date);
                            placeApply.setApply_period(listtime.get(l));
                            listfinal.add(placeApply);
                        }
                    }
                }
            }else {
                Date date = ObtainDate.tomorrowDate();//获取明天时间
                List<String> listtime = new ArrayList<>();
                if(placetest.getFtimeList().length==0){
                    listtime = ObtainDate.timeList();
                }else {
                    //获取时间链表
                    for(int g=0;g<placetest.getFtimeList().length;g++){
                        listtime.add(placetest.getFtimeList()[g]);
                    }
                }
                //获取预约链表
                List<PlaceApply> list = placeApplyService.QueryPlaceApplyByPage(listaim.get(i).getPlace_no(),date);

                //进行时间段匹配
                for ( int j = 0;j < list.size();j++){
                    for(int k = 0 ; k<listtime.size(); k++ ){
                        if(list.get(j).getApply_period().equals(listtime.get(k))){
                            listtime.remove(k);
                        }
                    }
                }
                if(listtime.size() != 0){
                    for ( int l = 0 ; l<listtime.size();l++){
                        PlaceApply placeApply = new PlaceApply();
                        placeApply.setPlace_no(listaim.get(i).getPlace_no());
                        placeApply.setPlace_type(listaim.get(i).getPlace_type());
                        placeApply.setPlace_storey(listaim.get(i).getPlace_storey());
                        placeApply.setApply_date(date);
                        placeApply.setApply_period(listtime.get(l));
                        listfinal.add(placeApply);
                    }
                }
            }
        }
        return listfinal;
    }


    //预约场地--教务场地
    @RequestMapping("/placeApplyAction")
    @ResponseBody
    public int placeApplyAction(@RequestBody PlaceApply placeApply,HttpSession session){
        placeApply.setUser_id((String) session.getAttribute("number"));
        placeApply.setApply_state("待使用");
        placeApply.setApply_type("教务");
        placeApplyService.InsertPlaceApply(placeApply);
        return 1;
    }

    //预约场地--普通用户
    @RequestMapping("/placeApplyAction1")
    @ResponseBody
    public int placeApplyAction1(@RequestBody PlaceApply placeApply, HttpSession session){
        placeApply.setUser_id((String) session.getAttribute("number"));
        placeApply.setApply_state("待审核");
        placeApply.setApply_type("正常");
        placeApplyService.InsertPlaceApply(placeApply);
        return 1;
    }

    //预约场地--赛事
    @RequestMapping("/placeApplyAction2")
    @ResponseBody
    public int placeApplyAction2(@RequestBody List<PlaceApply> applies, HttpSession session){
        System.out.println("dd"+applies);
        for(int i = 0;i<applies.size();i++){
            PlaceApply placeApply = applies.get(i);
            placeApply.setUser_id((String) session.getAttribute("number"));
            placeApply.setEventid("00000000");
            placeApply.setApply_state("待使用");
            placeApply.setApply_type("赛事");
            placeApplyService.InsertPlaceApply(placeApply);
        }
        return 1;
    }

    //赛事场地安排查询
    @GetMapping("/eventPlaceQuery")
    @ResponseBody
    public List<PlaceApply> event(String eventid){
        return placeApplyService.SelectEventPlace(eventid);
    }

    /*
    * 场地预约审核
    *
    * */
    //跳转到预约审核
    @GetMapping("/toPlaceAudit")
    public String toPlaceAudit(){
        return "placemodal/placeaudit";
    }

    //得到待审核数据
    @GetMapping("/getPlaceAudit")
    @ResponseBody
    public PlaceTest1 getPlaceAudit(Integer pageNum){
        //查询要审核的详细
        PlaceTest1 placeTest1 = new PlaceTest1();
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<PlaceApply> placeApplies = placeApplyService.QueryDismissPlaceApply("待审核");
        PageInfo page = new PageInfo(placeApplies);
        placeTest1.setPages(page);
        placeTest1.setPlaceApplies(placeApplies);
        return placeTest1;
    }

    //得到筛选后的待审核数据
    @GetMapping("/getPlaceAudit1")
    @ResponseBody
    public PlaceTest1 getPlaceAudit1(Integer pageNum, String placestorey, String placetype){
        //查询要审核的详细
        PlaceTest1 placeTest1 = new PlaceTest1();
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<PlaceApply> placeApplies = placeApplyService.QueryPlaceApplyByUser(null,null,"待审核",placetype,placestorey);
        PageInfo page = new PageInfo(placeApplies);
        placeTest1.setPages(page);
        placeTest1.setPlaceApplies(placeApplies);
        return placeTest1;
    }

    //审核
    @GetMapping("/aduitPlace")
    @ResponseBody
    public int aduitPlace(Integer apply_id,String state){
        placeApplyService.DeletePlaceApply(apply_id,state);
        return 1;
    }


    /*
    * 场地一周信息查询
    *
    * */
    //去场地一周信息查询页面
    @GetMapping("/toFindWeekInfo")
    public String toFindWeekInfo(){
        return "placemodal/findplaceweek";
    }

    //得到场地一周信息
    @GetMapping("/getPlaceWeekInfo")
    @ResponseBody
    public PlaceTest1 getPlaceWeekInfo(Integer pageNum){
        PlaceTest1 placeTest1 = new PlaceTest1();
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<PlaceApply> placeApplies = placeApplyService.QueryWeekApplyById(ObtainDate.tomorrowDate(),ObtainDate.weekDate(),null,null,null);
        PageInfo page = new PageInfo(placeApplies);
        placeTest1.setPages(page);
        placeTest1.setPlaceApplies(placeApplies);
        return placeTest1;
    }

    /*
     * 预约失约处理
     *
     * */
    @GetMapping("/getMissInfo")
    @ResponseBody
    public List<PlaceApply> getMissInfo(){
        return placeApplyService.QueryDismissPlaceApply("已失约");
    }

    @GetMapping("/dealDissMiss")
    @ResponseBody
    public int dealDissMiss(Integer applyid){
        placeApplyService.DeletePlaceApply(applyid,"已使用");
        return 1;
    }


    /*
    * 场地收费标准
    *
    * */
    //去场地收费标准页面
    @GetMapping("/toPlaceFeeStander")
    public String toPlaceFeeStander(){
        return "placemodal/placefeestander";
    }

    //查询场地收费标准
    @GetMapping("/getFeeStander")
    @ResponseBody
    public PlaceTest1 getFeeStander(){
        PlaceTest1 placeTest1 = new PlaceTest1();
        List<PlaceFeeStander> placeFeeStanders =  placeFeeStanderService.QueryPlaceFeeStander(null);
        placeTest1.setFeeStanders(placeFeeStanders);
        return placeTest1;
    }

    //新增场地收费标准
    @PostMapping("/addPlaceFeeStander")
    public String addPlaceFeeStander(PlaceFeeStander placeFeeStander,HttpSession session){
        placeFeeStander.setStander_maker((String) session.getAttribute("username"));
        placeFeeStander.setStander_state("正常");
        placeFeeStanderService.InsertPlaceStander(placeFeeStander);
        return "placemodal/placefeestander";
    }

    //删除场地收费标准
    @GetMapping("/deletePlaceFeeStander")
    @ResponseBody
    public String deletePlaceFeeStander(Integer feeid){
        placeFeeStanderService.UpdatePlaceStander(feeid);
        return "success";
    }

    //检测场地收费标准是否已存在
    @RequestMapping("/checkPlaceFeeStander")
    @ResponseBody
    public boolean checkPlaceFeeStander(String placefee){
        return placeFeeStanderService.SelectPlaceFeeStander(placefee);
    }

    @GetMapping("/toStanderDetail")
    public String toStanderDetail(Model model){
        List<PlaceFeeStander> list = placeFeeStanderService.QueryPlaceFeeStander(null);
        model.addAttribute("Placefeestanders",list);
        return "placemodal/standerdetail";
    }

    @PostMapping("/updatePlaceFeeStander")
    public String updatePlaceFeeStander(PlaceFeeStander placeFeeStander,@RequestParam("id") Integer id,HttpSession session){
        placeFeeStander.setFeestander_id(id);
        placeFeeStander.setStander_maker((String) session.getAttribute("username"));
        placeFeeStanderService.UpdatePlaceFeeStander1(placeFeeStander);
        return "placemodal/placefeestander";
    }


    /*
    * 场地使用
    *
    * */
    //去场地使用页面
    @GetMapping("/toPlaceUse")
    public String toPlaceUse(){
        return "placemodal/placeuse";
    }

    //得到用户的待使用场地信息
    @GetMapping("/getPlaceUseInfo")
    @ResponseBody
    public PlaceTest1 getPlaceUseInfo(Integer pageNum,HttpSession session){
        PlaceTest1 placeTest1 = new PlaceTest1();
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<PlaceApply> placeApplies = placeApplyService.SelectPlaceApplyByNow((String) session.getAttribute("number"),ObtainDate.nowDate());
        PageInfo page = new PageInfo(placeApplies);
        placeTest1.setPages(page);
        placeTest1.setPlaceApplies(placeApplies);
        return placeTest1;
    }

    //场地使用
    @GetMapping("/placeUser")
    @ResponseBody
    public String placeUser(Integer applyid,String time){
        //获取开始使用时间并记录
        placeApplyService.UpdatePlaceApply(applyid,time);
        return "success";
    }

    //去场地使用页面
    @GetMapping("/toUserPlaceApply")
    public String toUserPlaceApply(){
        return "placemodal/userplaceapply";
    }

    //得到用户的可退订场地预约信息
    @GetMapping("/getApplyEnCancel")
    @ResponseBody
    public PlaceTest1 getApplyEnCancel(Integer pageNum,HttpSession session){
        PlaceTest1 placeTest1 = new PlaceTest1();
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<PlaceApply> placeApplies = placeApplyService.SelectPlaceApplyByUser((String) session.getAttribute("number"),ObtainDate.nowDate());
        PageInfo page = new PageInfo(placeApplies);
        placeTest1.setPages(page);
        placeTest1.setPlaceApplies(placeApplies);
        return placeTest1;
    }

    @GetMapping("/getDisables")
    @ResponseBody
    public List<PlaceApply> getDisables(HttpSession session){
        return placeApplyService.QueryPlaceApplyByUser((String)session.getAttribute("number"),null,"审核未通过",null,null);
    }

    //取消预约
    @GetMapping("/cancelApply")
    @ResponseBody
    public int cancelApply(Integer applyid){
        placeApplyService.DeletePlaceApply(applyid,"已退订");
        return 1;
    }

    /*
    * 场馆公告
    *
    * */
    //去场馆公告页面
    @GetMapping("/toPlaceNotice")
    public String toPlaceNotice(){
        return "placemodal/placenotice";
    }

    //新增场馆公告
    @PostMapping("/addPlaceNotice")
    public String addPlaceNotice(PlaceNotice placeNotice,HttpSession session){
        placeNotice.setNotice_date(new Date());
        User user = (User) session.getAttribute("user");
        placeNotice.setNotice_state("正常");
        placeNotice.setUser_name(user.getUserName());
        placeNotice.setNotice_date(ObtainDate.nowDate());
        placeNoticeService.InsertPlaceNotice(placeNotice);
        return "placemodal/placenotice";
    }

    //查询场馆公告
    @GetMapping("/getPlaceNotice")
    @ResponseBody
    public List<PlaceNotice> getPlaceNotice(){
        return placeNoticeService.SelectAllPlaceNotice();
    }

    @GetMapping("/deletePlaceNotice")
    @ResponseBody
    public int deletePlaceNotice(Integer noticeid){
        placeNoticeService.DeletePlaceNotice(noticeid);
        return 1;
    }

}
